package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.SysDictType;
import com.cloudfun.campusshare.common.model.vo.SystemDictionaryVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.entity.SystemDictionaryEntity;
import com.cloudfun.campusshare.repo.SystemDictionaryEntityRepo;
import com.cloudfun.campusshare.util.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import javax.validation.Valid;
import java.util.Optional;

/**
 * @author : ashsr.zhang
 * @date : 2020/3/3 17:17
 * @Version : v1.0
 * @description 字典表接口
 **/
@RestController
@RequestMapping("/dictionary")
@Api(value = "系统字典管理")
@Validated
@Slf4j
@AuthRequired
public class DictionaryController {

    @Autowired
    private SystemDictionaryEntityRepo systemDictionaryEntityRepo;

    @ApiOperation(value = "系统字典增加", notes = "系统字典增加")
    @PostMapping("/add")
    public BaseResult addDictionary(@ApiParam(value = "systemDictionaryVO", required = true) @Valid SystemDictionaryVO systemDictionaryVO) {
        SystemDictionaryEntity entity = BeanCopyUtil.copy(systemDictionaryVO, SystemDictionaryEntity.class);
        systemDictionaryEntityRepo.save(entity);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @ApiOperation(value = "系统字典", notes = "系统字典列表")
    @GetMapping("/list")
    public BaseResult listDictionary(Pageable pageable, @ApiParam(value = "关键字") String keyword) {
        Page<SystemDictionaryEntity> page = systemDictionaryEntityRepo.findAll((root, criteriaQuery, criteriaBuilder) -> {
            Predicate[] pre = new Predicate[2];
            if (StringUtils.isNotBlank(keyword)) {
                pre[0] = criteriaBuilder.like(root.get("name"), "%" + keyword + "%");
            }

            return criteriaBuilder.and(pre);
        }, pageable);

        PageResult pageResult = new PageResult(page.getTotalElements(), page.getContent());
        return BaseResultFactory.produceResult(Code.SUCCESS, pageResult);
    }

    @ApiOperation(value = "获取字典值", notes = "获取字典值")
    @GetMapping("/key/get")
    public BaseResult getValueByKey(String key) {
        SystemDictionaryEntity byKey = systemDictionaryEntityRepo.findByKey(key).stream().findAny().get();
        return BaseResultFactory.produceResult(Code.SUCCESS, Optional.ofNullable(byKey)
                .map(SystemDictionaryEntity::getValue).orElseThrow(() -> new RuntimeException("该key不存在对应value")));
    }

    @ApiOperation(value = "系统字典删除", notes = "系统字典删除")
    @PostMapping("/delete")
    public BaseResult deleteDictionary(@RequestParam String dictionaryId) {
        systemDictionaryEntityRepo.deleteById(dictionaryId);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @ApiOperation(value = "获取字典值", notes = "获取字典值")
    @GetMapping
    public BaseResult<?> getValueByTypeAndKey(SysDictType type, String key) {
        SystemDictionaryEntity byKey = systemDictionaryEntityRepo.findByTypeAndKey(type, key);
        return BaseResultFactory.produceResult(Code.SUCCESS, Optional.ofNullable(byKey)
                .map(SystemDictionaryEntity::getValue).orElseThrow(() -> new RuntimeException("该key不存在对应value")));
    }
}
